import 'package:buttons_tabbar/buttons_tabbar.dart';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../assets/styles.dart';
import '../../../components/base_refresh/base_refresh_tab_widget.dart';
import '../../../components/no_more/no_data_masonry_grid_view.dart';
import '../../../components/no_more/no_data_sliver_list.dart';
import '../../../components/tab_bar_indicator/easy_fixed_indicator.dart';
import '../../../utils/color.dart';
import '../../../utils/extension.dart';
import '../common/ai_record_cell.dart';
import '../common/ai_record_cell_block.dart';
import '../controllers/ai_record_page_controller.dart';

class AiRecordPage extends GetView<AiRecordPageController> {
  const AiRecordPage({super.key});

  AppBar _buildAppBar() => AppBar(title: const Text('记录'));

  Widget _buildTypeTabBar() => SizedBox(
        height: 30.w,
        child: TabBar(
            tabs: AiRecordPageController.typeTabs
                .map((e) => Tab(text: e))
                .toList(),
            labelStyle: TextStyle(
              color: COLOR.white,
              fontSize: 16.w,
              fontWeight: FontWeight.w600,
            ),
            unselectedLabelStyle: TextStyle(
              fontSize: 16.w,
              color: COLOR.white.withValues(alpha: 0.6),
            ),
            tabAlignment: TabAlignment.start,
            isScrollable: true,
            indicator: EasyFixedIndicator(
              width: 20.w,
              height: 3.w,
              borderRadius: Styles.borderRadius.all(12.w),
              color: COLOR.color_FF9000,
            ),
            indicatorPadding: EdgeInsets.only(bottom: -1.w),
            padding: EdgeInsets.zero,
            indicatorWeight: 0,
            dividerColor: COLOR.transparent,
            dividerHeight: 0,
            labelPadding: EdgeInsets.symmetric(horizontal: 16.w)),
      );

  Widget _buildTypeTabBarView() => TabBarView(
        children: AiRecordPageController.typeTabs
            .mapIndexed(
              (i, _) => _buildStatusTab(i),
            )
            .toList(),
      );

  Widget _buildStatusTabBar(int typeIndex) => ButtonsTabBar(
        tabs:
            AiRecordPageController.statusTabs.map((e) => Tab(text: e)).toList(),
        labelStyle: TextStyle(
          color: COLOR.color_FF9205,
          fontSize: 14.w,
          fontWeight: FontWeight.w500,
        ),
        unselectedLabelStyle: TextStyle(
          fontSize: 14.w,
          color: COLOR.white,
          fontWeight: FontWeight.w500,
        ),
        borderColor: COLOR.color_FF9205,
        unselectedBorderColor: const Color(0x66ffffff),
        borderWidth: 1,
        backgroundColor: const Color(0x15ffffff),
        unselectedBackgroundColor: const Color(0x15ffffff),
        radius: 16.5.w,
        contentPadding: EdgeInsets.symmetric(horizontal: 8.w),
        buttonMargin: EdgeInsets.symmetric(horizontal: 5.w),
        height: 30.w,
        contentCenter: true,
        controller: controller.getStatusTabController(typeIndex),
      );

  Widget _buildStatusTabBarRow(int typeIndex) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        _buildStatusTabBar(typeIndex),
        Obx(
          () => controller.statusTabControllerIndexes[typeIndex] ==
                  AiRecordPageController.statusTabFailureIndex
              ? Text(
                  '删除',
                  style: TextStyle(color: COLOR.color_666666, fontSize: 14.w),
                ).onTap(() => controller.onTapDeleteStatus(typeIndex))
              : const SizedBox.shrink(),
        ),
      ],
    );
  }

  Widget _buildStatusTabBarView(int typeIndex) => TabBarView(
        controller: controller.getStatusTabController(typeIndex),
        children: AiRecordPageController.statusTabs.mapIndexed((i, _) {
          return _buildStatusContent(typeIndex, i).keepAlive;
        }).toList(),
      );

  Widget _buildStatusTab(int typeIndex) => Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _buildStatusTabBarRow(typeIndex).baseMarginHorizontal,
          18.verticalSpaceFromWidth,
          Expanded(
            child: _buildStatusTabBarView(typeIndex).baseMarginHorizontal,
          ),
        ],
      );

  Widget _buildStatusContent(int typeIndex, int statusIndex) {
    final tabKey = AiRecordTabKey(typeIndex, statusIndex);
    if (statusIndex == AiRecordPageController.statusTabSuccessIndex) {
      // 瀑布流
      return BaseRefreshTabWidget(
        controller,
        tabKey: tabKey,
        child: Obx(
          () {
            final data = controller.getData<AiRecordCellOption>(tabKey);
            return NoDataMasonryGridView.count(
              crossAxisCount: 2,
              itemBuilder: (ctx, i) => AiRecordCell.auto(option: data[i]),
              itemCount: data.length,
              mainAxisSpacing: 6.w,
              crossAxisSpacing: 7.w,
              noData: controller.dataInited(tabKey),
            );
          },
        ),
      );
    }
    // block
    return BaseRefreshTabWidget(
      controller,
      tabKey: tabKey,
      child: CustomScrollView(
        slivers: [
          Obx(() {
            final data = controller.getData<AiRecordCellOption>(tabKey);
            final blocks = data.slices(AiRecordCellBlock.recordMax).toList();
            return NoDataSliverList.separated(
              itemCount: blocks.length,
              itemBuilder: (ctx, i) => AiRecordCellBlock(options: blocks[i]),
              separatorBuilder: (ctx, i) => 10.verticalSpaceFromWidth,
              noData: controller.dataInited(tabKey),
            );
          })
        ],
      ),
    );
  }

  Widget _buildBody() => DefaultTabController(
        length: AiRecordPageController.typeTabs.length,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildTypeTabBar(),
            10.verticalSpaceFromWidth,
            Expanded(child: _buildTypeTabBarView()),
          ],
        ),
      );

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: _buildAppBar(),
        body: _buildBody(),
      );
}
